package com.kky.pp.adapter

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import androidx.core.util.Pair
import androidx.recyclerview.widget.ItemTouchHelper
import com.custom.base.base.BaseRecyclerAdapter
import com.custom.base.util.IntentUtil
import com.custom.base.view.SuperViewHolder
import com.kky.pp.R
import com.kky.pp.bean.WhoLikeMeListBean
import com.kky.pp.callback.WLMSwipedCallBack
import com.kky.pp.activity.user.UserProfileInfoActivity
import com.kky.pp.util.GlideUtil
import com.kky.pp.view.TagCloudView
import com.kky.pp.view.delay.DelayClickImageView
import io.rong.imkit.event.FirebaseEventTag
import io.rong.imkit.utils.FirebaseEventUtils


class WhoLikeMeAdapter(
    listModel: ArrayList<WhoLikeMeListBean>,
    mActivity: Activity,
    var wLMSwipedCallBack: WLMSwipedCallBack
) : BaseRecyclerAdapter<WhoLikeMeListBean>(listModel, mActivity) {
    override val layoutId: Int get() = R.layout.item_who_like_me
    private var isMember: Boolean = false

    fun setMember(member: Boolean) {
        this.isMember = member
    }

    override fun onBindItemHolder(
        position: Int,
        holder: SuperViewHolder,
        model: WhoLikeMeListBean,
        payloads: List<Any>?
    ) {
        val imgContainer = holder.getView<ConstraintLayout>(R.id.conItemLikeMeContainer)
        val itemInfoContainer = holder.getView<ConstraintLayout>(R.id.conItemLikeMeInfoContainer)
        val outSideContainer = holder.getView<ConstraintLayout>(R.id.conItemLikeMeOutSideContainer)
        val itemUserBg = holder.getView<ImageView>(R.id.imgViewItemLikeMeUserBg)
        val itemOnline = holder.getView<TextView>(R.id.txtItemLikeMeOnline)
        val itemPositioning = holder.getView<TextView>(R.id.txtItemLikeMePositioning)
        val itemName = holder.getView<TextView>(R.id.txtItemLikeMeName)
        val itemTagCloud = holder.getView<TagCloudView>(R.id.tagCloudItemLikeMe)
        val itemUserDislike = holder.getView<DelayClickImageView>(R.id.imgViewItemLikeMeUnLike)
        val itemUserLike = holder.getView<DelayClickImageView>(R.id.imgViewItemLikeMeLike)
        val itemNoMember = holder.getView<ImageView>(R.id.imgViewItemLikeMeEmpty)
        val txtTurnOnsSize = holder.getView<TextView>(R.id.txtItemLikeMeTurnOnsSize)
        GlideUtil.setImageView(
            model.avatarUrl,
            itemUserBg,
            !isMember,
            placeholderImageId = R.mipmap.icon_image_loading
        )
        if (model.turnOnsListSize != null && model.turnOnsListSize > 0) {
            txtTurnOnsSize.visibility = View.VISIBLE
            itemTagCloud.visibility = View.GONE
            txtTurnOnsSize.text =
                "${model.turnOnsListSize} ${mActivity.getString(R.string.turns_ons)}"
        } else {
            txtTurnOnsSize.visibility = View.GONE
            itemTagCloud.visibility = View.VISIBLE
            itemTagCloud.setTagBeansCheck(model.interests)
        }

        itemNoMember.visibility = if (isMember) View.GONE else View.VISIBLE

        itemUserDislike.visibility = if (isMember) View.VISIBLE else View.GONE
        itemUserLike.visibility = if (isMember) View.VISIBLE else View.GONE
        itemOnline.visibility = if (model.online == 1) View.VISIBLE else View.GONE
        itemPositioning.visibility = if (model.nearby == 1) View.VISIBLE else View.GONE

        itemName.text = "${model.nickName} · ${model.age}"
        itemUserDislike.setOnClickListener {
            wLMSwipedCallBack.swipedCallback(ItemTouchHelper.START, model)
        }
        itemUserLike.setOnClickListener {
            wLMSwipedCallBack.swipedCallback(ItemTouchHelper.END, model)
        }

        txtTurnOnsSize.setOnClickListener {
            if (isMember) {
                val bundle = Bundle()
                bundle.putBoolean("isWlm", true)
                bundle.putInt("selectPosition", position)
                bundle.putString("userCodeFriend", model.userCodeFriend)
                bundle.putBoolean("turnOpen", true)
                IntentUtil.startActivity(UserProfileInfoActivity::class.java, bundle)
            }
        }

        holder.setOnClickListener {
            if (isMember) {
                val bundle = Bundle()
                bundle.putBoolean("isWlm", true)
                bundle.putInt("selectPosition", position)
                bundle.putString("userCodeFriend", model.userCodeFriend)
                val pair1: Pair<View, String> = Pair.create(itemUserBg, itemUserBg.transitionName)
                val optionsCompat1: ActivityOptionsCompat =
                    ActivityOptionsCompat.makeSceneTransitionAnimation(mActivity, pair1)
                val intent = Intent(mActivity, UserProfileInfoActivity::class.java)
                intent.putExtra("url", model.avatarUrl)
                intent.putExtras(bundle)
                ActivityCompat.startActivity(mActivity, intent, optionsCompat1.toBundle())
                FirebaseEventUtils.logEvent(FirebaseEventTag.WLM_Profile.name)
            } else {
                wLMSwipedCallBack.swipedCallback(ItemTouchHelper.END, model)
            }
        }

        val imgParams = imgContainer.layoutParams as ConstraintLayout.LayoutParams
        val itemInfoParams = itemInfoContainer.layoutParams as ConstraintLayout.LayoutParams
        if (position % 2 == 0) {
            imgParams.startToEnd = ConstraintLayout.LayoutParams.UNSET
            imgParams.endToEnd = ConstraintLayout.LayoutParams.UNSET
            imgParams.endToStart = R.id.conItemLikeMeInfoContainer
            imgParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID


            itemInfoParams.startToStart = ConstraintLayout.LayoutParams.UNSET
            itemInfoParams.endToStart = ConstraintLayout.LayoutParams.UNSET
            itemInfoParams.startToEnd = R.id.conItemLikeMeContainer
            itemInfoParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
        } else {
            itemInfoParams.startToEnd = ConstraintLayout.LayoutParams.UNSET
            itemInfoParams.endToEnd = ConstraintLayout.LayoutParams.UNSET
            itemInfoParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID
            itemInfoParams.endToStart = imgContainer.id

            imgParams.startToStart = ConstraintLayout.LayoutParams.UNSET
            imgParams.endToStart = ConstraintLayout.LayoutParams.UNSET
            imgParams.startToEnd = itemInfoContainer.id
            imgParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID

        }
        imgContainer.layoutParams = imgParams
        itemInfoContainer.layoutParams = itemInfoParams

    }
}